<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Formatter for messages.
 *
 *  History:
 *  2006-08-05  Gelin   Initially created, methods moved from Formatter.php.
 *
 */

/**
 *  Formatter for messages.
 */
class MessageFormatter {

    /**
     *  Prepares message to display.
     *  Escapes HTML tags, ...
     *  @param  $message    message to prepare
     *  @return modified message
     */
    function prepareToDisplay($message) {
        if (isset($message['post_time'])) {
            $message['post_time'] = $this->formatTime($message['post_time']);
        }
        if (isset($message['name'])) {
            $message['name'] = $this->formatName($message['name']);
        }
        if (isset($message['email'])) {
            $message['email'] = $this->formatEmail($message['email']);
        }
        if (isset($message['url'])) {
            $message['url'] = $this->formatUrl($message['url']);
        }
        if (isset($message['message'])) {
            $message['message'] = $this->formatMessage($message['message']);
        }
        if (isset($message['reply_time'])) {
            $message['reply_time'] = $this->formatTime($message['reply_time']);
        }
        if (isset($message['reply'])) {
            $message['reply'] = $this->formatMessage($message['reply']);
        }
        return $message;
    }

    /**
     *  Formats time string.
     *  @param  $timestamp timestamp from database
     *  @return string represetation of time
     */
    function formatTime($timestamp) {
        global $config;
        return date($config['format']['time'], $timestamp);
    }

    /**
     *  Formats user's name, which posted message.
     *  @param  $name   username got from database
     *  @return formatted username to display on page
     */
    function formatName($name) {
        $name = strip_tags($name);
        return $name;
    }

    /**
     *  Formats user's email.
     *  @param  $email  email got from database
     *  @return formatted email to display on page
     */
    function formatEmail($email) {
        global $config;
        $email = strip_tags($email);
        $email = str_replace('@', $config['format']['email']['@'], $email);
        return $email;
    }

    /**
     *  Formats user's URL.
     *  @param  $url    URL got from database
     *  @return formatted URL to display on page
     */
    function formatUrl($url) {
        global $config;
        $url = strip_tags($url);
        $href = $url;
        if (!preg_match('/^[a-z]+:\/\/.+/', $url)) {
            $href = 'http://'.$url;
        }
        $target = $config['format']['url']['target'];
        $class = $config['format']['url']['class'];
        return "<a href=\"$href\" target=\"$target\" class=\"$class\">$url</a>";
    }

    /**
     *  Formats user's message.
     *  @param  $message    string message got from database
     *  @return formatted message to display on page
     */
     function formatMessage($message) {
        global $config;
        $message = strip_tags($message, $config['format']['message']['allow_tags']);
        $message = nl2br($message);
        return $message;
     }

}

?>